package com.heima.article.controller.v1;

import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.common.dtos.PageResponseResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.article.dtos.ArticleBehaviorDto;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.dtos.WmCommentApplyDto;
import com.heima.model.wemedia.dtos.WmCommentDto;
import com.heima.model.wemedia.dtos.WmCommentStatusDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/article")
public class ApArticleController {
    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    /**
     * 新增或修改文章
     */
    @PostMapping("/saveApArticle")
    public ResponseResult<Long> saveApArticle(@RequestBody ArticleDto dto){
        return ResponseResult.okResult(apArticleService.saveOrUpdateApArticle(dto));
    }

    /**
     * 查询所有App文章
     */
    @GetMapping("/findAll")
    public ResponseResult<List<ApArticle>> findAll(){
        return ResponseResult.okResult(apArticleService.list());
    }

    /**
     * 根据id查询文章
     */
    @GetMapping("/findArticleById/{id}")
    public ResponseResult<ApArticle> findArticleById(@PathVariable("id") Long id){
        return ResponseResult.okResult(apArticleService.getById(id));
    }


    /**
     * 根据用户ID 查询文章
     */
    @GetMapping("/findArticleByUserId/{uid}")
    public ResponseResult<List<ApArticle>>  findArticleByUserId(@PathVariable("uid") Long uid){
        List<ApArticle> apArticleList =  apArticleService.findArticleByUserId(uid);
        return ResponseResult.okResult(apArticleList);
    }
    /**
     * 根据分页对象 查询文章聚合
     */
    @PostMapping("/findArticleByUserId/Page")
    public PageResponseResult findArticlePage(@RequestBody WmCommentApplyDto dto){
       return   apArticleService.findArticlePage(dto);

    }

    /**
     * 根据用户id查询点赞数量，图文发布数量，收藏数量
     */
    @PostMapping("/findArticleByUserId")
    public ResponseResult<Map<String,Integer>> findArticleNumByUserId(@RequestBody ArticleBehaviorDto dto){
        return ResponseResult.okResult(apArticleService.getByUserId(dto));
    }

    /**
     * 根据文章Id修改文章的可评论状态
     */
    @PostMapping("UpdateArticleConfigStatus/status")
    public  ResponseResult updateArticleConfigStatus(@RequestBody WmCommentStatusDto dto){
               return    apArticleConfigService.updateArticleConfigStatus(dto);
    }


    /**
     * 根据用户id,起止时间查询文章详情
     * @param dto
     * @return
     */
    @PostMapping("/findByUserIdAndTime")
    public PageResponseResult findByUserIdAndTime(@RequestBody ArticleBehaviorDto dto){
        return apArticleService.findArticleByUserIdAndTime(dto);
    }
}
